package com.hhb.test;

import com.hhb.config.SpringConfig;
import com.hhb.dao.UserDao;
import com.hhb.dao.impl.UserDaoImpl;
import com.hhb.service.UserService;
import com.hhb.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class Demo02 {

    @Test
    public void test01(){

        Class[] interfaces = {UserDao.class};
        //第三个参数可以使用匿名内部类
        /*Proxy.newProxyInstance(Demo02.class.getClassLoader(), interfaces, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return null;
            }
        });*/
        UserDao userDao = new UserDaoImpl();
        UserDao dao = (UserDao) Proxy.newProxyInstance(Demo02.class.getClassLoader(), interfaces, new UserDaoProxy(userDao));
        int and = dao.and(1, 2);
        System.out.println(and);
        String id = dao.update("hcfs");
        System.out.println(id);


    }



    class UserDaoProxy implements InvocationHandler{

        //有参构造传递
        private Object obj;
        public UserDaoProxy(Object obj){
            this.obj=obj;
        }

        //增强的逻辑
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

            //执行方法前
            System.out.println("执行前……"+method.getName()+"::传递的参数…"+ Arrays.toString(args));

            //执行的方法
            Object res = method.invoke(obj, args);

            //执行方法后
            System.out.println("执行后……"+obj.toString());


            return res;
        }
    }
}
